<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>规则中心配置页面</title>
    <header th:replace="header::html"></header>
    <link th:href="@{/xadmin/lib/layui/css/layui.css}" rel="stylesheet" type="text/css"/>
    <script th:src="@{/xadmin/lib/layui/layui.js}" type="text/javascript"></script>
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-row  layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <form class="layui-form  layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="rtitle" id="rtitle" placeholder="请输入规则标题关键字" autocomplete="off"
                                       class="layui-input">
                            </div>
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" id="rtime" placeholder="请选择发布日期"
                                           name="rtime">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" id="rcontent" placeholder="请输入规则内容关键字" name="rcontent">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" id="uname" placeholder="请输入操作人员名称" name="uname">
                                </div>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="search"><i
                                        class="layui-icon">&#xe615;</i></button>
                            </div>

                        </form>
                        <br>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" onclick="add()"><i
                                    class="layui-icon">新增&#xe654;</i></button>
                        </div>
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-hide" id="ruleTable" lay-filter="ruleTable" ></table>
                    </div>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="modify" >修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>

    <script type="text/html" id="ruleFrame">
        <div style="padding:50px;font-size: 15px">
            <div>规则标题：<i id="tt" style="color: red;font-size: 12px"></i><textarea name="titles" id="titles" cols="50" rows="1" style="resize: none;font-size: 20px;border-radius: 5px" onblur="rcverify(0)"></textarea></div><br>
            <div>规则内容：<i id="cc" style="color: red;font-size: 12px"></i><textarea name="contents" id="contents" cols="50" rows="3" style="resize: none;font-size: 20px;border-radius: 5px" onblur="rcverify(1)"></textarea></div><br>
            <div style="float: right"><button class="layui-btn layui-btn-xs layui-btn-radius" onclick="addrule()">确定新增</button></div>
        </div>
    </script>
</body>
<script>

    var verify1=0;
    var verify2=0;
    var verify3=0;
    var verify4=0;
    var backTitle;
    var backContent;
    var opAdd;
    var opUpdata;
    var rid;
	layui.use('laydate', function () {
		var laydate = layui.laydate;
		//选中后的回调
		laydate.render({
			elem: '#rtime'
			, done: function (value, date) {
				// layer.alert('你选择的日期是：' + value + '<br>获得的对象是' + JSON.stringify(date));
			}
		});

	});
	layui.use(['table','form','layer'], function () {
		var table = layui.table,
            layer=layui.layer,
			form = layui.form;
		table.render({
			elem: '#ruleTable'
			, url: '/api-back/rule/ruleTable' //数据接口
			, page: true //开启分页
			, cellMinWidth: 80 //全局定义常规单元格的最小宽度
			, limits:[10,20]
			, limit:10
			, response: {
				dataName: 'datas' //规定数据列表的字段名称，默认：data
			}
			, cols: [
				[
					{type:'numbers' ,align:'center'}
					,{field:'rid', align:'center',title: '规则id',style:'display:none;'}
					,{field:'rtitle', title: '规则标题',width: '20%',align:'center',style:'color:red;'}
					,{field:'rcontent',align:'center',width: '35%',title: '规则内容'}
					,{field:'rtime', align:'center',width: '12%', title: '最后操作时间'}
					,{field:'uname', align:'center',width: '13%', title: '操作人员'}
					,{ fixed: 'right', title: '操作',  align: 'center',width: 180, toolbar: '#barDemo'}
				]
			],
			done: function () {
				//checkPermission();
			}

		});
		$('table.layui-table thead tr th:eq(1)').addClass('layui-hide');//隐藏的曝光表id表头
		//监听行工具事件
		table.on('tool(ruleTable)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
			var data = obj.data ,//获得当前行数据
				layEvent = obj.event; //获得 lay-event 对应的值
                rid=data.rid;
			if(layEvent === 'delete') {
				layer.confirm('确定要删除该条规则吗？删除后不可恢复',function (index) {
					$.ajax({
						type:"post",
						url:'/api-back/rule/ruleDelete',
						data:{"rid":rid},
						success:function (msg) {
							if (msg === "success") {
								layer.msg("删除成功",{icon:1});
								$(".layui-laypage-btn")[0].click();
							} else {
								layer.msg("删除失败，请重试",{icon:2})
							}
						},
						error:function (msg) {
							layer.open({
								type:2,
								content: "/404.html",
								area:['1000px','600px'],
								title:'权限不允许进行访问'
							})
						}
					})
				})
			}
			else if(layEvent === 'modify'){
				$.ajax({
					url:'/api-back/rule/ruleReview',
					type:"post",
					data:{"rid":rid},
					success:function (msg) {
						backTitle=msg.rtitle;
						backContent=msg.rcontent;
						rid=msg.rid;
						opUpdata=layer.open({
							type: 1 //Page层类型
							,area: ['600px', '400px']
							,title: '修改规则'
							,shade: 0.3 //遮罩透明度
							,anim: 1 //0-6的动画形式，-1不开启
							,content:'<div style="padding:50px;font-size: 15px">\n' +
								'            <div>规则标题：<i id="ttu" style="color: red;font-size: 12px"></i><textarea name="titleu" id="titleu" cols="50" rows="1" style="resize: none;font-size: 20px;border-radius: 5px" onblur="rcverify(2)">'+msg.rtitle+'</textarea></div><br>\n' +
								'            <div>规则内容：<i id="ccu" style="color: red;font-size: 12px"></i><textarea name="contentu" id="contentu" cols="50" rows="3" style="resize: none;font-size: 20px;border-radius: 5px" onblur="rcverify(3)">'+msg.rcontent+'</textarea></div><br>\n' +
								'            <div style="float: right"><button class="layui-btn layui-btn-xs layui-btn-radius" onclick="updataRule()">提交修改</button></div>\n' +
								'        </div>'
						});
					},
					error:function (msg) {
						layer.open({
							type:2,
							content: "/404.html",
							area:['1000px','600px'],
							title:'权限不允许进行访问'
						})
					}
				})
			}
		});

		//搜索
		form.on('submit(search)', function (data) {
			var field = data.field;
            table.render({
                elem: '#ruleTable'
                , url: '/api-back/rule/ruleTable' //数据接口
                , page: true //开启分页
	            , limits:[10,20]
	            , limit:10
                , where: {
                    "uname": field.uname,
                    "rtime": field.rtime,
                    "rcontent": field.rcontent,
                    "rtitle":field.rtitle
                }
                , response: {
                    dataName: 'datas' //规定数据列表的字段名称，默认：data
                }
                , cols: [
		            [
			            {type:'numbers' ,align:'center'}
			            ,{field:'rid', align:'center',title: '规则id',style:'display:none;'}
			            ,{field:'rtitle', title: '规则标题',width: '20%',align:'center',style:'color:red;'}
			            ,{field:'rcontent',align:'center',width: '35%',title: '规则内容'}
			            ,{field:'rtime', align:'center',width: '12%', title: '最后操作时间'}
			            ,{field:'uname', align:'center',width: '13%', title: '操作人员'}
			            ,{ fixed: 'right', title: '操作',  align: 'center',width: 180, toolbar: '#barDemo'}
		            ]
                ]
            });
            $('table.layui-table thead tr th:eq(1)').addClass('layui-hide');//隐藏的曝光表id表头
			return false;
		});
		//checkPermission();
	});

	function add(){
		opAdd=layer.open({
			type: 1 //Page层类型
			,area: ['600px', '400px']
			,title: '新增规则窗口'
			,shade: 0.3 //遮罩透明度
			,anim: 1 //0-6的动画形式，-1不开启
			,content:$("#ruleFrame").html()
		});
	}

	function addrule() {
		rcverify(0);
		rcverify(1);
		var titles=$("#titles").val();
		titles=titles.replace(/^\s+|\s+$/g,"");
		var contents=$("#contents").val();
		contents=contents.replace(/^\s+|\s+$/g,"");
		if (verify1 === 1 || verify2 === 1) {
			layer.msg("新增输入长度不合法", {icon: 2});
		} else {
			$.ajax({
				type:"post",
				url:'/api-back/rule/ruleAdd',
				data:{"titles":titles,"contents":contents},
				success:function (msg) {
					if (msg === "success") {
						layer.msg("添加成功",{icon:1});
						$(".layui-laypage-btn")[0].click();
						layer.close(opAdd);
					} else {
						layer.msg("已有相同标题的规则，请重新输入",{icon:2});
						document.getElementById("tt").innerText ="已有相同标题的规则，请重新输入"
					}
				},
				error:function (msg) {
					layer.open({
						type:2,
						content: "/404.html",
						area:['1000px','600px'],
						title:'权限不允许进行访问'
					})
				}
			})
		}
	}

    function updataRule() {
	    rcverify(2);
	    rcverify(3);
	    var titleu=$("#titleu").val();
	    titleu=titleu.replace(/^\s+|\s+$/g,"");
	    var contentu=$("#contentu").val();
	    contentu=contentu.replace(/^\s+|\s+$/g,"");
	    if (verify3 === 1 || verify4 === 1) {
		    layer.msg("输入长度不合法", {icon: 2});
	    } else if (titleu===backTitle&&contentu===backContent) {
		    layer.msg("您未对规则的标题和内容进行更改，不会为您提交至数据库", {icon: 2});
		    layer.close(opUpdata);
	    } else {
		    $.ajax({
			    type: "post",
			    url: '/api-back/rule/ruleUpdata',
			    data: {"rid":rid,"titles": titleu, "contents": contentu, "backTitle": backTitle},
			    success: function (msg) {
				    if (msg === "success") {
					    layer.msg("修改成功", {icon: 1});
					    $(".layui-laypage-btn")[0].click();
					    layer.close(opUpdata);
				    } else if (msg==="not") {
					    layer.msg("网络加载较慢，请重新操作", {icon: 2});
				    } else {
					    layer.msg("已有相同标题的规则，请重新输入", {icon: 2});
					    document.getElementById("ttu").innerText = "已有相同标题的规则，请重新输入"
				    }
			    },
			    error: function (msg) {
				    layer.open({
					    type:2,
					    content: "/404.html",
					    area:['1000px','600px'],
					    title:'权限不允许进行访问'
				    })
			    }
		    });
	    }
    }

	function rcverify(msg) {
		if (msg === 0) {
			var titles=$("#titles").val();
			titles=titles.replace(/^\s+|\s+$/g,"");
			if (titles.length >= 30||titles.length === 0) {
				document.getElementById("tt").innerText = "规则标题长度规定1-30个字符，当前长度：" + titles.length;
			    verify1=1;
			} else {
				document.getElementById("tt").innerText ="";
                verify1=0;
			}
		} else if(msg === 1) {
			var contents=$("#contents").val();
			contents=contents.replace(/^\s+|\s+$/g,"");
			if (contents.length>=200||contents.length === 0) {
				document.getElementById("cc").innerText="规则内容规定1-200个字符，当前长度："+contents.length;
			    verify2=1;
			} else {
				document.getElementById("cc").innerText ="";
                verify2=0;
			}
		} else if (msg === 2) {
			var titleu=$("#titleu").val();
			titleu=titleu.replace(/^\s+|\s+$/g,"");
			if (titleu.length >= 30||titleu.length === 0) {
				document.getElementById("ttu").innerText = "规则标题长度规定1-30个字符，当前长度：" + titleu.length;
				verify3=1;
			} else {
				document.getElementById("ttu").innerText ="";
				verify3=0;
			}
		} else if(msg === 3) {
			var contentu=$("#contentu").val();
			contentu=contentu.replace(/^\s+|\s+$/g,"");
			if (contentu.length>=200||contentu.length === 0) {
				document.getElementById("ccu").innerText="规则内容规定1-200个字符，当前长度："+contentu.length;
				verify4=1;
			} else {
				document.getElementById("ccu").innerText ="";
				verify4=0;
			}
		}

	}
</script>
</html>